package io.terminus.paas.profiler.model

import com.alibaba.dubbo.common.Constants
import com.alibaba.dubbo.common.URL
import io.terminus.paas.profiler.utils.ConvertUtil
import io.terminus.paas.profiler.model.Provider
import java.util.*

/**
 * Created by gengrong on 16/3/28.
 */
class DubboProvider : Provider() {

    var url: String? = null /* 提供者提供服务的地址 */

    var parameters: String? = null /* 提供者提供服务的参数 */

    var registry: String? = null/* 提供者连接的注册中心地址 */

    var dynamic: Boolean = false          /* 是否为动态注册服务 */

    var enabled: Boolean = false          /* 是否启用 */


    var weight: Int = 0          /* 权重 */

    var application: String? = null /* 应用名 */

    var username: String? = null      /* 提供者用户名 */

    var expired: Date? = null   /*过期时间*/

    var alived: Long = 0    /*存活时间，单位秒*/

    fun toUrl(): URL {
        val serviceName2Map = ConvertUtil.serviceName2Map(service!!)
        /*if(!serviceName2Map.containsKey(Constants.INTERFACE_KEY)) {
            throw new IllegalArgumentException("No interface info");
        }
        if(!serviceName2Map.containsKey(Constants.VERSION_KEY)) {
            throw new IllegalArgumentException("No version info");
        }*/

        val u = url
        var url = URL.valueOf(u + "?" + parameters)

        url = url.addParameters(serviceName2Map)

        val dynamic = dynamic
        if (!dynamic) {
            url = url.addParameter(Constants.DYNAMIC_KEY, false)
        }
        val enabled = enabled
        if (enabled != url.getParameter("enabled", true)) {
            if (enabled) {
                url = url.removeParameter("enabled")
            } else {
                url = url.addParameter("enabled", false)
            }
        }

        return url
    }
}